package com.frostmourne.mxsguimaker.objects;

import com.gitee.mxstarsex.objects.builders.bukkit.item.ItemBuilderNeo;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class PAPIHooker extends PlaceholderExpansion {

    @Override
    public String onPlaceholderRequest(Player player, String params) {


        if (params.contains("_")){

            String[] s = params.split("_");
            String type = s[0];
            PlayerData data = PlayerData.getPlayerData(player);

            switch (type.toLowerCase()){

                case "forever-variable":
                    return String.valueOf(data.getForeverObject(s[1]));
                case "temp-variable":
                    return String.valueOf(data.getTempObject(s[1]));
                case "item":
                    try {
                        String inv = s[1];
                        int slot = Integer.parseInt(s[2]);
                        ItemBuilderNeo item = new ItemBuilderNeo(data.getItem(inv, slot));
                        if (item.isNull()){
                            return "0";
                        }
                        switch (s[3].toLowerCase()){

                            case "display":
                                return item.hasDisplay() ? item.getDisplay() : item.getType().name();
                            case "lore":
                                if (item.hasLore()){
                                    int line = Integer.parseInt(s[4]);
                                    try {
                                        return item.getLore().get(line);
                                    }catch (ArrayIndexOutOfBoundsException e){
                                        return "None has line '" + line + "'";
                                    }
                                }
                            case "nbt":
                                try {
                                    String key = s[4];
                                    return item.getTags().getOrDefault(key,"0");
                                }catch (ArrayIndexOutOfBoundsException e){
                                    return "nbt key not found";
                                }

                            default:
                                break;
                        }

                    }catch (ArrayIndexOutOfBoundsException e){
                        return "Unknown Arguments";
                    }

                default:
                    break;
            }

        }

        return "0";

    }

    @Override
    public String getIdentifier() {
        return "mxsgm";
    }

    @Override
    public String getAuthor() {
        return "Frostmourne";
    }

    @Override
    public String getVersion() {
        return "1.0";
    }
}
